package org.jeecg.modules.dock.handler.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.dock.entity.DockTaskRecordMap;
import org.jeecg.modules.dock.entity.mes.request.*;
import org.jeecg.modules.dock.entity.mes.response.MesBaseResponseVO;
import org.jeecg.modules.dock.enums.MesInterfaceTypeEnum;
import org.jeecg.modules.dock.handler.MesLogicHandler;
import org.jeecg.modules.dock.service.IDockTaskRecordMapService;
import org.jeecg.modules.dock.service.MesDockingService;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * mes请求接口统一处理实现类
 *
 * @author zhoutong
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class MesLogicHandlerImpl implements MesLogicHandler {

    private final MesDockingService mesDockingService;

    private final IDockTaskRecordMapService dockTaskRecordMapService;

    @Override
    public synchronized MesBaseResponseVO handle(String businessCode, String requestVO) {
        log.info("request-------------->{}", requestVO);
        MesBaseRequestVO mesBaseRequestVO = JSON.parseObject(requestVO, MesBaseRequestVO.class);
        // 判断MES发送过来的任务是否已经发送过, 如果发送过就提示不可重复发送
        if (!MesInterfaceTypeEnum.KH_STOCK_REPLY.getCode().equals(businessCode)) {
            List<DockTaskRecordMap> dockTaskRecordMaps = dockTaskRecordMapService.list(new LambdaQueryWrapper<DockTaskRecordMap>()
                    .eq(DockTaskRecordMap::getMesTaskCode, mesBaseRequestVO.getTaskCode()));
            if (dockTaskRecordMaps.size() > 0) {
                return MesBaseResponseVO.failure("已经推送过这个任务, 不可重复推送!!!");
            }
        }
        if (MesInterfaceTypeEnum.KH_STOCK.getCode().equals(businessCode)) {
            ScoringStorageOrScratchRequestVO vo = JSON.parseObject(requestVO, ScoringStorageOrScratchRequestVO.class);
            log.info("刻痕入库------->{}", vo);
            return mesDockingService.scoringStorage(vo);
        } else if (MesInterfaceTypeEnum.HJ_TAKE.getCode().equals(businessCode)) {
            CrossShearCallMaterialRequestVO vo = JSON.parseObject(requestVO, CrossShearCallMaterialRequestVO.class);
            log.info("横剪叫料vo------->{}", vo);
            return mesDockingService.crossShearCallMaterial(vo);
        } else if (MesInterfaceTypeEnum.HJ_RETURN.getCode().equals(businessCode)) {
            CrossShearingMaterialWithdrawalRequestVO vo = JSON.parseObject(requestVO, CrossShearingMaterialWithdrawalRequestVO.class);
            log.info("横剪退料vo------->{}", vo);
            return mesDockingService.crossShearingMaterialWithdrawal(vo);
        } else if (MesInterfaceTypeEnum.HJ_TAKE_PLANK.getCode().equals(businessCode)) {
            TransverseShearCallMaterialPlateRequestVO vo = JSON.parseObject(requestVO, TransverseShearCallMaterialPlateRequestVO.class);
            log.info("横剪叫料板vo------->{}", vo);
            return mesDockingService.transverseShearCallMaterialPlate(vo);
        } else if (MesInterfaceTypeEnum.HJ_TAKE_WASTE.getCode().equals(businessCode)) {
            TransverseShearCalledScrapBoxRequestVO vo = JSON.parseObject(requestVO, TransverseShearCalledScrapBoxRequestVO.class);
            log.info("横剪叫废料箱vo------->{}", vo);
            return mesDockingService.transverseShearCalledScrapBox(vo);
        } else if (MesInterfaceTypeEnum.HJ_RETURN_WASTE.getCode().equals(businessCode)) {
            CrossCutTheScrapBinRequestVO vo = JSON.parseObject(requestVO, CrossCutTheScrapBinRequestVO.class);
            log.info("横剪退废料箱vo------->{}", vo);
            return mesDockingService.crossCutTheScrapBin(vo);
        } else if (MesInterfaceTypeEnum.HJ_STOCK.getCode().equals(businessCode)) {
            ScissorEntryRequestVO vo = JSON.parseObject(requestVO, ScissorEntryRequestVO.class);
            log.info("横剪入库vo------->{}", vo);
            return mesDockingService.scissorEntry(vo);
        } else if (MesInterfaceTypeEnum.DP_TAKE.getCode().equals(businessCode)) {
            LaminationCallRequestVO vo = JSON.parseObject(requestVO, LaminationCallRequestVO.class);
            log.info("叠片叫料vo------->{}", vo);
            return mesDockingService.laminationCall(vo);
        } else if (MesInterfaceTypeEnum.DP_RETURN.getCode().equals(businessCode)) {
            LaminationReturnRequestVO vo = JSON.parseObject(requestVO, LaminationReturnRequestVO.class);
            log.info("叠片退料vo------->{}", vo);
            return mesDockingService.laminationReturn(vo);
        } else if (MesInterfaceTypeEnum.DP_TAKE_REPLY.getCode().equals(businessCode)) {
            LaminationCallMaterialCompletionReturnColumnMaterialDetails vo = JSON.parseObject(requestVO, LaminationCallMaterialCompletionReturnColumnMaterialDetails.class);
            log.info("叠片叫料完成wms返回柱料明细vo------->{}", vo);
            return mesDockingService.laminationCallMaterialCompletionReturn(vo);
        } else if (MesInterfaceTypeEnum.DP_RETURN_PLANK.getCode().equals(businessCode)) {
            LaminatedRefeedingPlateRequestVO vo = JSON.parseObject(requestVO, LaminatedRefeedingPlateRequestVO.class);
            log.info("叠片退料板vo------->{}", vo);
            return mesDockingService.laminatedRefeedingPlate(vo);
        } else if (MesInterfaceTypeEnum.ZJ_TAKE.getCode().equals(businessCode)) {
            SlittingIsCalledMaterialRequestVO vo = JSON.parseObject(requestVO, SlittingIsCalledMaterialRequestVO.class);
            log.info("纵剪叫料vo------->{}", vo);
            return mesDockingService.slittingIsCalledMaterial(vo);
        } else if (MesInterfaceTypeEnum.ZJ_RETURN.getCode().equals(businessCode)) {
            TheMaterialIsWithdrawnByLongitudinalShearingRequestVO vo = JSON.parseObject(requestVO, TheMaterialIsWithdrawnByLongitudinalShearingRequestVO.class);
            log.info("纵剪退料vo------->{}", vo);
            return mesDockingService.theMaterialIsWithdrawnByLongitudinalShearing(vo);
        } else if (MesInterfaceTypeEnum.ZJ_STOCK.getCode().equals(businessCode)) {
            LongitudinalShearStorageRequestVO vo = JSON.parseObject(requestVO, LongitudinalShearStorageRequestVO.class);
            log.info("纵剪入库------->{}", vo);
            return mesDockingService.longitudinalShearStorage(vo);
        } else if (MesInterfaceTypeEnum.ZJ_TAKE_PAPER.getCode().equals(businessCode)) {
            VerticalCuttingIsCalledPaperTubeRequestVO vo = JSON.parseObject(requestVO, VerticalCuttingIsCalledPaperTubeRequestVO.class);
            log.info("纵剪叫纸筒------->{}", vo);
            return mesDockingService.verticalCuttingIsCalledPaperTube(vo);
        } else if (MesInterfaceTypeEnum.ZJ_RETURN_PAPER.getCode().equals(businessCode)) {
            VerticalAndHorizontalCuttingRequestVO vo = JSON.parseObject(requestVO, VerticalAndHorizontalCuttingRequestVO.class);
            log.info("纵横剪退纸筒------->{}", vo);
            return mesDockingService.verticalAndHorizontalCutting(vo);
        } else if (MesInterfaceTypeEnum.DP_TAKE_AUXILIARY.getCode().equals(businessCode)) {
            LaminatedAuxiliaryMaterialOrderingRequestVO vo = JSON.parseObject(requestVO, LaminatedAuxiliaryMaterialOrderingRequestVO.class);
            log.info("叠片辅材叫料------->{}", vo);
            return mesDockingService.laminatedAuxiliaryMaterialOrdering(vo);
        } else if (MesInterfaceTypeEnum.DP_RETURN_AUXILIARY.getCode().equals(businessCode)) {
            LaminatedAuxiliaryMaterialReturnRequestVO vo = JSON.parseObject(requestVO, LaminatedAuxiliaryMaterialReturnRequestVO.class);
            log.info("叠片辅材退料------->{}", vo);
            return mesDockingService.laminatedAuxiliaryMaterialReturn(vo);
        } else if (MesInterfaceTypeEnum.BZ_TAKE.getCode().equals(businessCode)) {
            PackagingLineMaterialOrderingRequestVO vo = JSON.parseObject(requestVO, PackagingLineMaterialOrderingRequestVO.class);
            log.info("包装线叫料------->{}", vo);
            return mesDockingService.packagingLineMaterialOrdering(vo);
        } else if (MesInterfaceTypeEnum.BZ_RETURN.getCode().equals(businessCode)) {
            PackagingLineReturnRequestVO vo = JSON.parseObject(requestVO, PackagingLineReturnRequestVO.class);
            log.info("包装线退料------->{}", vo);
            return mesDockingService.packagingLineReturn(vo);
        } else if (MesInterfaceTypeEnum.CP_STOCK_DC.getCode().equals(businessCode)) {
            FinishedProductWarehousingScaleRequestVO vo = JSON.parseObject(requestVO, FinishedProductWarehousingScaleRequestVO.class);
            log.info("成品入库请求（定尺）------->{}", vo);
            return mesDockingService.finishedProductWarehousing(vo);
        } else if (MesInterfaceTypeEnum.CP_STOCK_TX.getCode().equals(businessCode)) {
            FinishedProductWarehousingIronRequestVO vo = JSON.parseObject(requestVO, FinishedProductWarehousingIronRequestVO.class);
            log.info("成品入库请求（铁心）------->{}", vo);
            return mesDockingService.finishedProductWarehousingIron(vo);
        } else if (MesInterfaceTypeEnum.KH_TAKE.getCode().equals(businessCode)) {
            ScoringReturnOrScratchRequestVO vo = JSON.parseObject(requestVO, ScoringReturnOrScratchRequestVO.class);
            log.info("刻痕叫料------->{}", vo);
            return mesDockingService.scoringScratch(vo);
        } else if (MesInterfaceTypeEnum.KH_RETURN.getCode().equals(businessCode)) {
            ScoringReturnOrScratchRequestVO vo = JSON.parseObject(requestVO, ScoringReturnOrScratchRequestVO.class);
            log.info("刻痕退料------->{}", vo);
            return mesDockingService.scoringReturn(vo);
        } else if (MesInterfaceTypeEnum.ZJ_RETURN_STEEL.getCode().equals(businessCode)) {
            ReturnSteelBarrelRequestVO vo = JSON.parseObject(requestVO, ReturnSteelBarrelRequestVO.class);
            log.info("纵剪退钢筒------->{}", vo);
            return mesDockingService.returnSteelBarrel(vo);
        } else if (MesInterfaceTypeEnum.ZJ_TAKE_TRAY.getCode().equals(businessCode)) {
            TheSlittingIsCalledATrayRequestVO vo = JSON.parseObject(requestVO, TheSlittingIsCalledATrayRequestVO.class);
            log.info("纵剪叫托盘------->{}", vo);
            return mesDockingService.theSlittingIsCalledATray(vo);
        } else if (MesInterfaceTypeEnum.ZJ_USE_PAPER.getCode().equals(businessCode)) {
            ZJUsePaperRequestVO vo = JSON.parseObject(requestVO, ZJUsePaperRequestVO.class);
            log.info("纵剪消耗纸筒------->{}", vo);
            return mesDockingService.zjConsumePaper(vo);
        } else if (MesInterfaceTypeEnum.HJ_TAKE_MANUAL.getCode().equals(businessCode)) {
            CrossShearCallMaterialRequestVO vo = JSON.parseObject(requestVO, CrossShearCallMaterialRequestVO.class);
            log.info("横剪叫料-手动vo------->{}", vo);
            return mesDockingService.crossShearCallMaterial(vo);
        } else if (MesInterfaceTypeEnum.SC_STOCK_SUPER.getCode().equals(businessCode)) {
            SCStockSuperRequestVO vo = JSON.parseObject(requestVO, SCStockSuperRequestVO.class);
            log.info("生产入库超大卷vo------->{}", vo);
            return mesDockingService.scStockSuper(vo);
        } else if (MesInterfaceTypeEnum.KH_STOCK_REPLY.getCode().equals(businessCode)) {
            UpdateScoringStorageRequestVO vo = JSON.parseObject(requestVO, UpdateScoringStorageRequestVO.class);
            log.info("刻痕入库更新信息vo------->{}", vo);
            return mesDockingService.updateScoringStorage(vo);
        } else if (MesInterfaceTypeEnum.BZ_TAKE_AUTO.getCode().equals(businessCode)) {
            PackagingLineMaterialOrderingRequestVO vo = JSON.parseObject(requestVO, PackagingLineMaterialOrderingRequestVO.class);
            log.info("包装叫料任务-自动完成vo------->{}", vo);
            return mesDockingService.packagingLineMaterialOrdering(vo);
        } else if (MesInterfaceTypeEnum.TB_QUALITY_SYNC.getCode().equals(businessCode)) {
            FinishedProductWarehousingIronRequestVO vo = JSON.parseObject(requestVO, FinishedProductWarehousingIronRequestVO.class);
            log.info("同步【铁心】质检结果------->{}", vo);
            return mesDockingService.syncExamIronResult(vo);
        } else if (MesInterfaceTypeEnum.BZ_TAKE_TRANS.getCode().equals(businessCode)) {
            StripMaterialIsTransferredOutOfStorageRequestVO vo = JSON.parseObject(requestVO, StripMaterialIsTransferredOutOfStorageRequestVO.class);
            log.info("MES-WMS发送条料调拨出库提醒vo------->{}", vo);
            return mesDockingService.stripMaterialIsTransferredOutOfStorage(vo);
        } else {
            return MesBaseResponseVO.failure("未找到该业务");
        }
    }

    @Override
    public MesBaseResponseVO checkInventory(String requestVO) {
        CheckInventoryRequestVO vo = JSON.parseObject(requestVO, CheckInventoryRequestVO.class);
        log.info("MES-WMS查询库存接口vo------->{}", vo);
        return null;
    }

}
